package com.iceframework.hiapp.core.utils;

import com.iceframework.hiapp.ResourceTable;
import com.iceframework.hiapp.core.constants.ResourceTableConstants;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.text.Layout;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

/**
 * Toast tip utils
 */
public class HiToastUtils {

    private static final int INFO = 1;
    private static final int WARNING = 2;
    private static final int ERROR = 3;

    private static DirectionalLayout initToastTheme(AbilitySlice abilitySlice){
        if(ResourceTableConstants.TOAST_LAYOUT != null){
            return (DirectionalLayout) LayoutScatter.getInstance(abilitySlice).parse(ResourceTableConstants.TOAST_LAYOUT, null, false);
        }
        return null;
    }

    /**
     * get ToastDialog Object
     * @param abilitySlice AbilitySlice
     */
    public static ToastDialog getToastDialog(AbilitySlice abilitySlice){
        return new ToastDialog(abilitySlice.getContext());
    }

    /**
     * show ToastDialog text
     * @param abilitySlice AbilitySlice
     * @param text show text
     * @param millisecond
     */
    public static void show(AbilitySlice abilitySlice, String text, int millisecond){
        DirectionalLayout directionalLayout = initToastTheme(abilitySlice);
        if(directionalLayout != null){
            Text toastText = (Text) directionalLayout.findComponentById(ResourceTable.Id_msg_toast);
            toastText.setText(text);
            ToastDialog toastDialog = new ToastDialog(abilitySlice.getContext());
            toastDialog.setText(text).setComponent(directionalLayout)
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setAlignment(LayoutAlignment.BOTTOM)
                    .setDuration(millisecond).show();
        } else {
            ToastDialog toastDialog = new ToastDialog(abilitySlice.getContext());
            toastDialog.setText(text).setDuration(millisecond).show();
        }
    }

    /**
     * show ToastDialog text
     * @param abilitySlice AbilitySlice
     * @param text show text
     * @param millisecond
     */
    private static void show(AbilitySlice abilitySlice, String text, int millisecond, int level){
        Color color = null;
        switch(level){
            case INFO: color = Color.WHITE;break;
            case WARNING: color = new Color(Color.rgb(255,185,15));break;
            case ERROR: color = new Color(Color.rgb(255,0,0));break;
            default: color = Color.WHITE;break;
        }
        DirectionalLayout directionalLayout = initToastTheme(abilitySlice);
        if(directionalLayout != null){
            Text toastText = (Text) directionalLayout.findComponentById(ResourceTable.Id_msg_toast);
            toastText.setTextColor(color);
            toastText.setText(text);
            ToastDialog toastDialog = new ToastDialog(abilitySlice.getContext());
            toastDialog.setText(text).setComponent(directionalLayout)
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setAlignment(LayoutAlignment.BOTTOM)
                    .setDuration(millisecond).show();
        } else {
            ToastDialog toastDialog = new ToastDialog(abilitySlice.getContext());
            toastDialog.setText(text).setDuration(millisecond).show();
        }
    }

    /**
     * show ToastDialog text
     * @param abilitySlice AbilitySlice
     * @param text show text
     * @param millisecond
     */
    public static void showInfo(AbilitySlice abilitySlice, String text, int millisecond){
        show(abilitySlice,text,millisecond, INFO);
    }

    /**
     * show ToastDialog text
     * @param abilitySlice AbilitySlice
     * @param text show text
     * @param millisecond
     */
    public static void showWarning(AbilitySlice abilitySlice, String text, int millisecond){
        show(abilitySlice,text,millisecond, WARNING);
    }

    /**
     * show ToastDialog text
     * @param abilitySlice AbilitySlice
     * @param text show text
     * @param millisecond
     */
    public static void showError(AbilitySlice abilitySlice, String text, int millisecond){
        show(abilitySlice,text,millisecond, ERROR);
    }

}
